package com.bosch.in.rtp1.ept.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import com.bosch.in.rtp1.ept.constant.CommonConstants;
import com.bosch.in.rtp1.ept.constant.HoldCodeType;
import com.bosch.in.rtp1.ept.model.dto.HoldCodeFilter;

public class HoldCodeUtility {

	public static final String MULTI = "MULTI";
	public static final String CLUSTER = "CLUSTER";
	public static final String REST = "REST";
	
	public static List<HoldCodeFilter> extractHoldCode(List<HoldCodeFilter> allHCFilters, HoldCodeType type) {
		List<HoldCodeFilter> hcFilters = new ArrayList<HoldCodeFilter>();
		
		for (HoldCodeFilter hcFilter : allHCFilters) {
			if (hcFilter.getType() == type) {
				// Clone object, so if we have to update object, cache item will not be changed.
				hcFilters.add(hcFilter.clone());
			}
		}
		
		
		return hcFilters;
	}

	/**
	 * 
	 * @param hcFilters
	 */
	public static void injectAllHCForRest(List<HoldCodeFilter> hcFilters) {
		// handle specical case for Hold Code Reclassified
		List<String> hcs = new ArrayList<String>();
		
		for (HoldCodeFilter hcFilter : hcFilters) {
			// CLUSTER --> MULTI
			if (hcFilter.getComment().equals(CLUSTER)) {
				hcFilter.setPatterns(StringUtility.extractHCPatterns(MULTI));
			}
			hcs.addAll(hcFilter.getPatterns());
		}
		
		for (HoldCodeFilter hcFilter : hcFilters) {
			// REST --> Ignore aLL HoldCode
			if (hcFilter.getComment().equals(REST)) {
				hcFilter.setPatterns(hcs);
			}
		}
	}
	
	public static boolean isRest(String[] parameterValues) {
		boolean isRest = false;
		for (String param : parameterValues) {
			if (param.equals(MULTI) && parameterValues.length > 1) {
				isRest = true;
				break;
			}
		}
		return isRest;
	}
	
	/**
	 * 
	 * @param allHCFilters
	 * @return
	 */
	public static String getHoldCodeStr(List<HoldCodeFilter> allHCFilters) {
		StringBuffer sb = new StringBuffer();
		for (HoldCodeFilter hcFilter : allHCFilters) {
			sb.append(hcFilter.getPatternsInStr().replaceAll(CommonConstants.DELIMITER, CommonConstants.COMMA));
		}
		return sb.toString();
	}
	
	/**
	 * Extract hold text from parameter value including both holdText + '+=+' + hold codes string
	 * @param txt
	 * @return hold text
	 */
	public static String extractHoldText(String txt) {
		String holdText = "";
		if (txt != null && !txt.equals("")) {
			Pattern p = Pattern.compile(CommonConstants.SEPARATOR, Pattern.LITERAL);
			List<String> result = Arrays.asList(p.split(txt));
			// if hold text indicates for selecting all , ignore it
			if (result.get(0).toUpperCase().indexOf(CommonConstants.SELECT_ALL) < 0) {
				holdText = result.get(0);
			}
		}
		
		return holdText;
	}
	
	/**
	 * Extract hold codes string from parameter value including both holdText + '+=+' + hold codes string 
	 * @param txt
	 * @return hold codes string
	 */
	public static String extractHoldCodes(String txt) {
		String holdCodes = "";
		if (txt != null && !txt.equals("")) {
			Pattern p = Pattern.compile(CommonConstants.SEPARATOR, Pattern.LITERAL);
			List<String> result = Arrays.asList(p.split(txt));
			if (result.size() == 2) {
				holdCodes = result.get(1);
			}
		}
		
		return holdCodes;
	}
}
